package com.dongao.activity;

import io.vov.vitamio.LibsChecker;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.MediaPlayer.OnCompletionListener;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.dongao.model.CourseWare;
import com.dongao.model.Mp3Info;
import com.dongao.utils.StringUtil;

public class VideoActivity extends Activity implements OnCompletionListener {

	private VideoView mVideoView;
	private ImageView mOperationBg;
	private View mVolumeBrightnessLayout;
	private ImageView mOperationPercent;
	/** 最大声音 */
	private int mMaxVolume;
	/** 当前声音 */
	private int mVolume = -1;
	/** 当前亮度 */
	private float mBrightness = -1f;
	private AudioManager mAudioManager;
	private GestureDetector mGestureDetector;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置成全屏模式

		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//强制为横屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		if (!LibsChecker.checkVitamioLibs(this)){
			return;
		}
		setContentView(R.layout.videoview);
		
		mVideoView = (VideoView)findViewById(R.id.surface_view);
		mVolumeBrightnessLayout = findViewById(R.id.operation_volume_brightness);
		mOperationBg = (ImageView) findViewById(R.id.operation_bg);
		mOperationPercent = (ImageView) findViewById(R.id.operation_percent);

		mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		
		CourseWare info = (CourseWare)getIntent().getSerializableExtra("mp3Info");
		if (info!=null && !StringUtil.isEmpty(info.getCwVideoUrl())) {
			//			mVideoView.setVideoURI(Uri.parse(info.getUrl()));
			mVideoView.setVideoPath(info.getCwVideoUrl());
			mVideoView.setMediaController(new MediaController(this));
			mVideoView.requestFocus();
		}
		
		mGestureDetector = new GestureDetector(this, new MyGestureListener());
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (mGestureDetector.onTouchEvent(event))
			return true;

		// 处理手势结束
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_UP:
			endGesture();
			break;
		}

		return super.onTouchEvent(event);
	}
	
	/** 手势结束 */
	private void endGesture() {
		mVolume = -1;
		mBrightness = -1f;
//
//		// 隐藏
		mDismissHandler.removeMessages(0);
		mDismissHandler.sendEmptyMessageDelayed(0, 500);
	}
	
	/** 定时隐藏 */
	private Handler mDismissHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			mVolumeBrightnessLayout.setVisibility(View.GONE);
		}
	};
	
	private class MyGestureListener extends SimpleOnGestureListener {
		/** 滑动 */
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
			float mOldX = e1.getX(), mOldY = e1.getY();
			System.out.println("mOldX="+mOldX);
			System.out.println("mOldY="+mOldY);
			int y = (int) e2.getRawY();
			System.out.println("y="+y);
			Display disp = getWindowManager().getDefaultDisplay();
			int windowWidth = disp.getWidth();
			int windowHeight = disp.getHeight();
			
			System.out.println("windowWidth="+windowWidth);
			System.out.println("windowHeight="+windowHeight);
			System.out.println(" windowWidth * 4.0 / 5="+ windowWidth * 4.0 / 5);
			
			if (mOldX > windowWidth * 4.0 / 5)// 右边滑动
				onVolumeSlide((mOldY - y) / windowHeight);
			else if (mOldX < windowWidth / 5.0)// 左边滑动
				onBrightnessSlide((mOldY - y) / windowHeight);

			return super.onScroll(e1, e2, distanceX, distanceY);
		}
		
		
		/**
		 * 滑动改变声音大小
		 * 
		 * @param percent
		 */
		private void onVolumeSlide(float percent) {
			if (mVolume == -1) {
				mVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
				if (mVolume < 0)
					mVolume = 0;

				// 显示
				mOperationBg.setImageResource(R.drawable.video_volumn_bg);
				mVolumeBrightnessLayout.setVisibility(View.VISIBLE);
			}

			int index = (int) (percent * mMaxVolume) + mVolume;
			if (index > mMaxVolume)
				index = mMaxVolume;
			else if (index < 0)
				index = 0;

			// 变更声音
			mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, index, 0);

			// 变更进度条
			ViewGroup.LayoutParams lp = mOperationPercent.getLayoutParams();
			lp.width = findViewById(R.id.operation_full).getLayoutParams().width * index / mMaxVolume;
			mOperationPercent.setLayoutParams(lp);
		}
		
		/**
		 * 滑动改变亮度
		 * 
		 * @param percent
		 */
		private void onBrightnessSlide(float percent) {
			if (mBrightness < 0) {
				mBrightness = getWindow().getAttributes().screenBrightness;
				if (mBrightness <= 0.00f)
					mBrightness = 0.50f;
				if (mBrightness < 0.01f)
					mBrightness = 0.01f;

				// 显示
				mOperationBg.setImageResource(R.drawable.video_brightness_bg);
				mVolumeBrightnessLayout.setVisibility(View.VISIBLE);
			}
			WindowManager.LayoutParams lpa = getWindow().getAttributes();
			lpa.screenBrightness = mBrightness + percent;
			if (lpa.screenBrightness > 1.0f)
				lpa.screenBrightness = 1.0f;
			else if (lpa.screenBrightness < 0.01f)
				lpa.screenBrightness = 0.01f;
			getWindow().setAttributes(lpa);

			ViewGroup.LayoutParams lp = mOperationPercent.getLayoutParams();
			lp.width = (int) (findViewById(R.id.operation_full).getLayoutParams().width * lpa.screenBrightness);
			mOperationPercent.setLayoutParams(lp);
		}
	}

	@Override
	public void onCompletion(MediaPlayer arg0) {
		finish();
	}
	
}
